package com.sskj.spk.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.sskj.spk.R;
import com.sskj.spk.bean.LevelVolumeBean;
import com.sskj.spk.presenter.TradePresenterBb;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 币种详情 --成交记录
 *
 * @author Hey
 * created at 2018/4/4 14:14
 */
public class TradeFragmentBb extends BaseFragment<TradePresenterBb> {
    @BindView(R.id.trade_recyclerView)
    RecyclerView tradeRecyclerView;
    private String code;
    private BaseAdapter adapter;
    private int page = 1;

    public TradeFragmentBb() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.app_fragment_trade;
    }

    @Override
    public TradePresenterBb getPresenter() {
        return new TradePresenterBb();
    }

    @Override
    public void initView() {
        tradeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        adapter = new BaseAdapter<LevelVolumeBean>(R.layout.app_recycler_item_trade, new ArrayList<>(), tradeRecyclerView) {
            @Override
            public void bind(ViewHolder injector, LevelVolumeBean item) {
                injector.setText(R.id.trade_time, item.getTime());
//                if (item.getType() == 1) {
//                    injector.setText(R.id.trade_type, App.INSTANCE.getString(R.string.strTradeFragment1));
//                    injector.setText(R.id.trade_price, NumberUtil.keep4(item.getBuyPrice()) + "");
//
//                } else {
//                    injector.setText(R.id.trade_type, App.INSTANCE.getString(R.string.strTradeFragment3));
//                    injector.setText(R.id.trade_price, NumberUtil.keep4(item.getSellPrice()) + "");
//
//                }
                injector.setText(R.id.trade_price, item.getOpen());
//                injector.setTextColor(R.id.trade_type, ContextCompat.getColor(getActivity(), item.getType() == 1 ? R.color.bull_cyan_blue : R.color.bull_red));
                injector.setText(R.id.trade_num, item.getVolume());
            }
        };
        adapter.setEmptyView();
        View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.app_layout_header_trade, null);
        adapter.addHeaderView(inflate);
        adapter.setOnLoadMoreListener(() -> {
            page++;
            mPresenter.getTradeList(code, page);
        }, tradeRecyclerView);
    }

    @Override
    public void initData() {
        code = getArguments().getString("code");
        mPresenter.getTradeList(code, 1);
    }


    public void setData(List<LevelVolumeBean> data) {
//        if (data.size() < 20) {
//            isLoadMore = false;
//            adapter.loadMoreEnd();
//        } else {
//            adapter.loadMoreComplete();
//        }
        adapter.loadMoreEnd();
        adapter.getData().clear();
        adapter.addData(data);
    }

    public static TradeFragmentBb newInstance(String code) {
        TradeFragmentBb fragment = new TradeFragmentBb();
        Bundle bundle = new Bundle();
        bundle.putString("code", code);
        fragment.setArguments(bundle);
        return fragment;
    }


}
